// pages/addressedit/addressedit.js
var app = getApp();
var utils = require('../../utils/util.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        region: "",
        id: ""

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this;
        console.log(options.id)
        if (options.id) {
            that.addressInfo();

        } else {
            that.setData({
                id: ""
            })
        }
    },
    //地址列表
    addressInfo: function () {
        var that = this;
        utils.http("/api/v1/storemanage/store_address", {
            store_id: wx.getStorageSync("userType").storeid
        }, function (res) {
            that.setData({
                defaultAddress: res.datas,
                region: res.datas.address.split(" ")
            })
        })
    },
    bindRegionChange(e) {
        console.log('picker发送选择改变，携带值为', e.detail.value)
        this.setData({
            region: e.detail.value
        })
    },
    formSubmit: function (e) {
        var that = this;
        console.log(e)
        var name = e.detail.value.name;
        var phone = e.detail.value.phone;
        var address = that.data.region;
        var detail = e.detail.value.address;
        if (name != "" && (/^1(3|4|5|7|8)\d{9}$/.test(phone)) && address != "" && detail != "") {
            utils.http("/api/v1/storemanage/add_modify_store_address", {
                name: name,
                mobile: phone,
                address: address,
                detail: detail,
                id: that.data.id,
                store_id: wx.getStorageSync("userType").storeid
            }, function (res) {
                console.log(res)
                if (res.state == 1) {
                    wx.navigateBack({})
                } else {
                    utils.message(res.datas);
                }
            })
        } else {
            if (detail == "") {
                wx.showToast({
                    title: '请输入详细地址',
                    icon: "none"
                })
            }
            if (address == "") {
                wx.showToast({
                    title: '请输入所在地址',
                    icon: "none"
                })
            }
            if (!(/^1(3|4|5|7|8)\d{9}$/.test(phone))) {
                wx.showToast({
                    title: '请输入正确的手机号',
                    icon: "none"
                })
            }
            if (name == "") {
                wx.showToast({
                    title: '请输入您的名字',
                    icon: "none"
                })
            }


        }

    },

})